package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LimitedReward {
  private static Logger _log = Logger.getLogger(LimitedReward.class.getName());
  
  private static final Log _logx = LogFactory.getLog(LimitedReward.class.getName());
  
  private static LimitedReward _instance;
  
  public static final HashMap<Integer, L1WilliamLimitedReward> _itemIdIndex = new HashMap<>();
  
  public static final HashMap<Integer, L1WilliamLimitedReward> _itemIdIndex1 = new HashMap<>();
  
  public static LimitedReward getInstance() {
    if (_instance == null)
      _instance = new LimitedReward(); 
    return _instance;
  }
  
  public static void reload() {
    LimitedReward oldInstance = _instance;
    _instance = new LimitedReward();
    _itemIdIndex.clear();
  }
  
  private LimitedReward() {
    loadChackSerial();
  }
  
  private static void loadChackSerial() {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM w_即时奖励系统");
      rs = pstm.executeQuery();
      fillChackSerial(rs);
    } catch (SQLException e) {
      _log.log(Level.SEVERE, "error while creating w_即时奖励系统 table", e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  private static void fillChackSerial(ResultSet rs) throws SQLException {
    PerformanceTimer timer = new PerformanceTimer();
    while (rs.next()) {
      int id = rs.getInt("id");
      boolean activity = (rs.getInt("activity") == 1);
      int check_classId = rs.getInt("check_classId");
      int check_level = rs.getInt("check_level");
      int check_item = rs.getInt("check_item");
      int check_itemCount = rs.getInt("check_itemCount");
      String surplus_msg = rs.getString("surplus_msg");
      int surplus_msg_color = rs.getInt("surplus_msg_color");
      String itemId = rs.getString("give_item_id");
      String count = rs.getString("give_itemCount");
      String enchantlvl = rs.getString("give_itemEnchantlvl");
      int totalCount = rs.getInt("total_People");
      int appearCount = rs.getInt("appear_People");
      int quest_id = rs.getInt("quest_id");
      int quest_step = rs.getInt("quest_step");
      String message = rs.getString("message");
      String message_end = rs.getString("message_end");
      L1WilliamLimitedReward armor_upgrade = new L1WilliamLimitedReward(id, activity, check_classId, 
          check_level, check_item, check_itemCount, surplus_msg, surplus_msg_color, itemId, count, enchantlvl, 
          totalCount, appearCount, quest_id, quest_step, message, message_end);
      _itemIdIndex.put(Integer.valueOf(id), armor_upgrade);
      _itemIdIndex1.put(Integer.valueOf(check_item), armor_upgrade);
    } 
    _logx.info("载入即时奖励系统数据资料数量: " + _itemIdIndex.size() + "(" + timer.get() + "ms)");
  }
  
  public L1WilliamLimitedReward[] getLimitedRewardList() {
    return (L1WilliamLimitedReward[])_itemIdIndex.values()
      .toArray((Object[])new L1WilliamLimitedReward[_itemIdIndex.size()]);
  }
  
  public L1WilliamLimitedReward getTemplate(int id) {
    return _itemIdIndex.get(Integer.valueOf(id));
  }
  
  public L1WilliamLimitedReward getTemplate1(int check_item) {
    return _itemIdIndex1.get(Integer.valueOf(check_item));
  }
  
  public static void limitedRewardToList(int id, long appearCount) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("UPDATE w_即时奖励系统 SET appear_People=? WHERE id=?");
      ps.setLong(1, appearCount);
      ps.setInt(2, id);
      ps.execute();
    } catch (SQLException e) {
      e.getLocalizedMessage();
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public static void limitedRewardToList_isOver(int id, int activity) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("UPDATE w_即时奖励系统 SET activity=? WHERE id=?");
      ps.setInt(1, activity);
      ps.setInt(2, id);
      ps.execute();
    } catch (SQLException e) {
      e.getLocalizedMessage();
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
}
